package org.dashboard.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.dashboard.bean.AdDataset;
import org.dashboard.bean.AdFolder;
import org.dashboard.bean.AdSchema;
import org.dashboard.constant.Constant;
import org.dashboard.service.AdDatasetService;
import org.dashboard.service.AdExcelService;
import org.dashboard.service.AdFolderService;
import org.dashboard.service.AdSchemaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service(interfaceClass = AdExcelService.class)
@CacheConfig(cacheManager = Constant.DEFAULT_CACHE_MANAGER)
public class AdExcelServiceImpl implements AdExcelService {

    @Autowired
    private AdSchemaService adSchemaService;

    @Autowired
    private AdDatasetService adDatasetService;

    @Autowired
    private AdFolderService adFolderSerice;

    @Override
    @Transactional
    public Integer[] importExcelSchema(String schemaName, String datasetName, String excelFileName, List<Object[]> excelData) {
        if (excelData.size() > 1) {
            List<Map<String, String>> schemaColumns = new ArrayList<>();
            for (Object obj : excelData.get(0)) {
                Map<String, String> schemaColumn = new HashMap<>();
                schemaColumn.put("Code", UUID.randomUUID().toString());
                schemaColumn.put("Name", obj.toString());
                schemaColumn.put("Column", obj.toString());
                schemaColumn.put("DataType", "");
                schemaColumn.put("AnalysisType", "");
                schemaColumns.add(schemaColumn);
            }
            for (int i = 0; i < excelData.get(1).length; i++) {
                if (schemaColumns.size() > 0) {
                    Map<String, String> schemaColumn = schemaColumns.get(i);
                    if (excelData.get(1)[i].getClass() == String.class) {
                        schemaColumn.put("DataType", "string");
                        schemaColumn.put("AnalysisType", "dimension");
                    } else if (excelData.get(1)[i].getClass() == double.class) {
                        schemaColumn.put("DataType", "int");
                        schemaColumn.put("AnalysisType", "measure");
                    } else if (excelData.get(1)[i].getClass() == Date.class) {
                        schemaColumn.put("DataType", "time");
                        schemaColumn.put("AnalysisType", "dimension");
                    } else {
                        schemaColumn.put("DataType", "string");
                        schemaColumn.put("AnalysisType", "dimension");
                    }
                }
            }

            AdFolder adFolder = adFolderSerice.selectAdFolderByFolderName("EXCEL导入数据");
            if (adFolder == null) {
                adFolder = new AdFolder();
                adFolder.setFolderName("EXCEL导入数据");
                adFolder.setFolderType(1);
                adFolder.setFolderParent(-1);
                adFolder.setFolderId(adFolderSerice.insert(adFolder));
            }

            AdSchema adSchema = new AdSchema();
            adSchema.setSchemaCode(UUID.randomUUID().toString());
            adSchema.setSchemaName(schemaName);
            adSchema.setSchemaFolderId(adFolder.getFolderId());
            adSchema.setSchemaData(JSON.toJSONString(schemaColumns));
            adSchema.setSchemaId(adSchemaService.insert(adSchema));

            Map<String, Object> datasetData = new HashMap<>();
            datasetData.put("DataServiceType", "excel");
            datasetData.put("DataColumnsMap", schemaColumns);
            Map<String, Object> DataServiceMeta = new HashMap<>();
            DataServiceMeta.put("ExcelFileName", excelFileName);
            datasetData.put("DataServiceMeta", DataServiceMeta);
            AdDataset adDataset = new AdDataset();
            adDataset.setDatasetCode(UUID.randomUUID().toString());
            adDataset.setDatasetName(datasetName);
            adDataset.setDatasetData(JSON.toJSONString(datasetData));
            adDataset.setSchemaId(adSchema.getSchemaId());
            adDataset.setDatasetId(adDatasetService.insert(adDataset));

            return new Integer[]{adSchema.getSchemaId(), adDataset.getDatasetId()};
        }
        return null;
    }
}
